package ua.net.gm.common.entity;

import java.util.Objects;

/**
 * Dynamic IDs only. Can't be used for synchronization or serialization.
 *
 * @author syar
 */
public class EntityId
{
    private final int id;
    
    private final EntityType type;

    private String label;

    protected EntityId(EntitySupport entitySupport, EntityType type)
    {
        this.type = type;
        this.id = entitySupport.getEntityIdFactory().createId(type);
    }

    public final EntityType getType()
    {
        return type;
    }

    public void setLabel(String label)
    {
        this.label = label;
    }

    public String getLabel()
    {
        if (label == null)
        {
            return toString();
        }
        return label;
    }

    public int getId()
    {
        return id;
    }

    @Override
    public String toString()
    {
        return getType().toString() + id;
    }

    @Override
    public int hashCode()
    {
        int hash = 7;
        hash = 17 * hash + this.id;
        hash = 17 * hash + Objects.hashCode(getType());
        return hash;
    }

    @Override
    public boolean equals(Object obj)
    {
        if (obj == null)
        {
            return false;
        }
        if (getClass() != obj.getClass())
        {
            return false;
        }
        final EntityId other = (EntityId) obj;
        if (this.id != other.id)
        {
            return false;
        }
        return Objects.equals(this.getType(), other.getType());
    }
}